package com.superteam.zzl.Des;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.superteam.zzl.activity.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/1/11.
 */
public class MyPutongAdapter extends BaseAdapter {
    private Context context;
    private List<Object> list;

    public MyPutongAdapter(Context context, List<Object> list) {
        this.context = context;
        this.list = list;
    }


    @Override
    public void notifyDataSetChanged() {

        //   Object object1 = list.get(0);


        super.notifyDataSetChanged();
    }

    @Override
    public int getCount() {

        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        Object object = list.get(position);
        if (object instanceof Putong2.Data.Recommend_pin_list) {
            return 1;//gv
        } else {
            return 0;//lv
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        Object object = list.get(position);

        if (convertView == null) {
            vh = new ViewHolder();

            if (getItemViewType(position) == 0) {

                convertView = View.inflate(context, R.layout.putong_listview, null);
                vh.listView1 = (MyListView) convertView.findViewById(R.id.pu_lv);
                //vh.listView2 = (MyListView) convertView.findViewById(R.id.pu_lv);
                vh.tv1 = (TextView) convertView.findViewById(R.id.ltv1);
                vh.tv2 = (TextView) convertView.findViewById(R.id.ltv2);
                if (object instanceof Putong2.Data.Cms_list) {
                    if (position==0){
                    vh.tv1.setText("发现新奇");
                    vh.tv2.setVisibility(View.INVISIBLE);}
                    else {
                        vh.tv1.setVisibility(View.INVISIBLE);
                        vh.tv2.setVisibility(View.INVISIBLE);
                    }
                }
//                else {
//                    vh.tv1.setText("奇");
//                    vh.tv2.setVisibility(View.VISIBLE);
//                }
                convertView.setTag(vh);

            } else if (getItemViewType(position) == 1) {
                // vh=new ViewHolder();
                convertView = View.inflate(context, R.layout.putong_gridview, null);
                vh.gridView = (MyGridView) convertView.findViewById(R.id.pu_gv);
                convertView.setTag(vh);

            }
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        if (getItemViewType(position) == 0) {
            if (object instanceof Putong2.Data.Cms_list) {
                final Putong2.Data.Cms_list pdc = (Putong2.Data.Cms_list) object;
                final List<Object> dc = new ArrayList<>();
                dc.add(pdc);
                PutongLAdapter1 adapter1 = new PutongLAdapter1(context, dc);
                Log.e("asdf", "up");
                adapter1.notifyDataSetChanged();
                vh.listView1.setAdapter(adapter1);
                adapter1.notifyDataSetChanged();
            } else if (object instanceof Putong2.Data.Index_trends) {
                final Putong2.Data.Index_trends pdi = (Putong2.Data.Index_trends) object;
                final List<Object> di = new ArrayList<>();
                di.add(pdi.trends_list.get(0));
                di.add(pdi.trends_list.get(1));
                di.add(pdi.group_user);

                PutongLAdapter2 adapter2 = new PutongLAdapter2(context, di);
                adapter2.notifyDataSetChanged();
                vh.listView1.setAdapter(adapter2);
                adapter2.notifyDataSetChanged();
            }
        } else if (getItemViewType(position) == 1) {
            if (object instanceof Putong2.Data.Recommend_pin_list) {
                final Putong2.Data.Recommend_pin_list pdr = (Putong2.Data.Recommend_pin_list) object;
                final List<Object> dr = new ArrayList<>();
                dr.addAll(pdr.entertainment);
                PutongAdapter adapter = new PutongAdapter(context, dr);
                adapter.notifyDataSetChanged();
                vh.gridView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }


        }
        return convertView;
    }

    class ViewHolder {
        MyGridView gridView;
        TextView tv1, tv2;
        MyListView listView1;
       // MyListView listView2;
    }
}
